home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte0787.arc / NEWGEN.ARC / FILEIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-01-27  |  2.9 KB  |  91 lines

  1. /* file reading and writing benchmark
  2.    sequentially writes a 65000 byte file on disk
  3.    generates random long numbers
  4.    uses these modulo 65000 to read and write strings of ODDNUM bytes
  5.    with the file handling system of the c package
  6.    the random number generator is set to a specific seed,
  7.    so that all compilers should generate the same code
  8. */
  9.  
  10. #define ERROR -1
  11. #define READERR 0
  12.  
  13. #define BEG 0
  14. #define CURR 1
  15. #define END 2
  16. #define READ 0
  17. #define WRITE 1
  18. #define UPDATE 2
  19.  
  20. #define OKCLOSE 0
  21. #define FILESIZE 65000L
  22. #define COUNT 500
  23.  
  24. #define C 13849L
  25. #define A 25173L
  26. #define ODDNUM 23
  27. long seed = 7L;
  28.  
  29. long random (), lseek ();
  30.  
  31. main ()
  32.         {
  33.         int i;
  34.         long j, pos;
  35.         int fd;
  36.         char buffer [ODDNUM + 1];
  37.  
  38.         if ((fd = creat ("test.dat", WRITE)) == ERROR)
  39.                 abort ("Can't create data file\n");
  40.         else    printf("File opened for sequential writing\n");
  41.         for (j = 0; j < FILESIZE; ++j)
  42.                 if (write(fd, "x", 1) == ERROR)
  43.                        abort ("Unexpected EOF in writing data file\n");
  44.         if (close (fd) != OKCLOSE)
  45.                 abort ("Error closing data file\n");
  46.         else
  47.                 printf ("Normal termination writing data file\n");
  48.         if ((fd = open ("test.dat", UPDATE)) == ERROR)
  49.                 abort ("Can't open data file for random reading and writing\n");
  50.         else    printf ("File opened for random reading and writing\n");
  51.         for (i = 0; i < COUNT; ++i)
  52.                 {
  53.                 j = random (FILESIZE);
  54.                 if (j < 0L)
  55.                         j = (-j);
  56.                 if (FILESIZE - j < ODDNUM)
  57.                         continue;
  58.                 if ((pos = lseek (fd, j, BEG)) == -1L)
  59.                         abort ("Error reading at random offset\n");
  60.                 if (read (fd, buffer, ODDNUM) == READERR)
  61.                         abort ("Error reading at random offset\n");
  62.                 j = random (FILESIZE);
  63.                 if (j < 0L)
  64.                         j = (-j);
  65.                 if (FILESIZE - j < ODDNUM)
  66.                         continue;
  67.                 if ((pos = lseek (fd, j, BEG)) == -1L)
  68.                         abort ("Error seeking to random offset\n");
  69.                 if (write (fd, buffer, ODDNUM) == READERR)
  70.                         abort ("Error writing at random offset\n");
  71.                 }
  72.         if (close (fd) != OKCLOSE)
  73.                 abort ("Error closing data file\n");
  74.         else
  75.                 printf ("Normal termination from random reading and writing\n");
  76.         }
  77.  
  78. long random (size)
  79.         long size;
  80.         {
  81.         seed = seed * A + C;
  82.         return (seed % size);
  83.         }
  84.  
  85. abort (message)
  86.         char *message;
  87.         {
  88.         printf (message);
  89.         exit (ERROR);
  90.         }